package chat;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * @Author GongXl
 * @Description 专门监听服务器的响应信息
 * @Date 001 10:26
 * @Param
 * @return
**/
public class ClientListener implements Runnable {
    private Socket socket;
    public ClientListener(Socket socket){
        this.socket = socket;
    }
    /**
     * @Author GongXl
     * @Description 按字节读数据，缓存流不能读
     * @Date 001 11:54
     * @Param []
     * @return void
    **/
    @Override
    public void run() {
        InputStream inputStream =null;
        try {
            byte[] ary = new byte[1024];
            inputStream = socket.getInputStream();
            int len = 0;
            while ((len = inputStream.read(ary)) != -1){
                String s = new String(ary,0,len);
                System.out.println("from server:"+socket+"say:"+s);
            }
            System.out.println("end");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(null != inputStream){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
